package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sg.hms.Model.User;
import com.sg.hms.Service.JisuangongziService;
import com.sg.hms.ServiceImpl.JisuangongziServiceImpl;
/**
 * 
 * @author 廖彦
 *
 */
public class JiesuanEmployeeSalaryController extends HttpServlet{
	JisuangongziService jisuangongzi = new JisuangongziServiceImpl();
     @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	doPost(req, resp);
    }
     @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	PrintWriter pw = resp.getWriter();
    	int temp = 1;//临时变量，用来判断数据库中的salaryDate是否有同年同月的，以免当月重复结算工资
    	int index = 0;
    	HttpSession session = req.getSession();
    	User user = (User)session.getAttribute("currentuser");
        int elId = user.getuId();//获取财务部当前操作主管的编号
        String date = req.getParameter("date"); //获取结算日期（当前日期）
        String salaryDate = req.getParameter("salaryDate");//获取工资日期（结算哪个月的工资）
        String salaryDate1[] = salaryDate.split("-");//分割字符串，返回一个字符串数组
        int s_elId =Integer.parseInt(req.getParameter("elId")); //获取当前被结算员工的编号
        List<String> list = jisuangongzi.findOldSalarySer(s_elId);
        String arr[] = new String[list.size()]; //定义一个String数组，长度为list的size()
        //把list中的salaryDate装入arr[]
        if (arr.length==0) {
        	SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
			java.sql.Date d = null;
			java.sql.Date s_d = null;
			try {
				Date d1 = sfd.parse(date);
				Date d2 = sfd.parse(salaryDate);
				d = new java.sql.Date(d1.getTime());
				s_d = new java.sql.Date(d2.getTime());
				jisuangongzi.jisuanSalarySer(elId, s_elId, d, s_d);
				pw.write("1");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		} 
        
        else {
			  for (String a : list) {
	              	arr[index] = a;
	              	index++;
	      		}
	        	 
			  for (int i = 0; i < arr.length; i++) {
	      			String arr1[] = arr[i].split("-");
	      			if (salaryDate1[0].equals(arr1[0])) { //判断是否同年
	      				if (salaryDate1[1].equals(arr1[1])) { //判断是否同月
	      					temp = 0; //如果有同年同月的记录，则置temp = 0
	      				}
	      			}
	      		}    
	        if (temp==0) {
				pw.write("0");
			} else {
				SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
				java.sql.Date d = null;
				java.sql.Date s_d = null;
				try {
					Date d1 = sfd.parse(date);
					Date d2 = sfd.parse(salaryDate);
					d = new java.sql.Date(d1.getTime());
					s_d = new java.sql.Date(d2.getTime());
					jisuangongzi.jisuanSalarySer(elId, s_elId, d, s_d);
					pw.write("1");
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			} 
		}
        
        
    }
}
